package com.moose.operator.web.service.impl.message;

import com.moose.operator.model.domain.ChatMessageDO;
import com.moose.operator.model.domain.UserInfoDO;
import com.moose.operator.model.dto.ChatMessageDTO;
import com.moose.operator.model.dto.UserInfoDTO;
import com.moose.operator.web.service.MessageService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * @author taohua
 */
@Service
public class MessageServiceImpl implements MessageService {

  @Override public ChatMessageDTO saveMessage(ChatMessageDTO chatMessageDTO) {
    ChatMessageDO chatMessageDO = new ChatMessageDO();
    BeanUtils.copyProperties(chatMessageDTO, chatMessageDO);
    UserInfoDTO fromUserInfoDTO = chatMessageDTO.getFrom();
    if (ObjectUtils.isNotEmpty(fromUserInfoDTO)) {
      UserInfoDO fromUserInfoDO = new UserInfoDO();
      BeanUtils.copyProperties(fromUserInfoDTO, fromUserInfoDO);
      chatMessageDO.setFrom(fromUserInfoDO);
    }

    UserInfoDTO toUserInfoDTO = chatMessageDTO.getTo();
    if (ObjectUtils.isNotEmpty(toUserInfoDTO)) {
      UserInfoDO toUserInfoDO = new UserInfoDO();
      BeanUtils.copyProperties(toUserInfoDTO, toUserInfoDO);
      chatMessageDO.setTo(toUserInfoDO);
    }

    // TODO: mongo or mysql save message ....
    return chatMessageDTO;
  }
}
